package cn.itsource.pethome.base.controller;

import cn.itsource.pethome.base.util.FastDfsApiOpr;
import cn.itsource.pethome.base.util.JsonResult;
import org.apache.commons.io.FilenameUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @PostMapping
    public JsonResult upload(@RequestPart(required = true,value = "file") MultipartFile file) throws IOException {
        try {
            //获取附件名
            String fileName = file.getOriginalFilename();
            //获取后缀名
            String extension = FilenameUtils.getExtension(fileName);
            //开始上传，返回上传路径
            String upload = FastDfsApiOpr.upload(file.getBytes(), extension);

            /*打印出来看看的*/
            JsonResult jsonResult = new JsonResult().setResult(upload);
            System.out.println(jsonResult);
            //返回一个json结果对象
            return new JsonResult().setResult(upload);
        } catch (Exception e) {
            e.printStackTrace();
            return new JsonResult(false, "上传失败!!");
        }
    }
}
